/*
 * @(#)EmployeeSqlMapDao.java   08/12/25
 *
 *
 */



package net.learntechnology.examples.dao.ibatis;

//~--- non-JDK imports --------------------------------------------------------

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.ibatis.sqlmap.client.SqlMapClient;

import net.learntechnology.examples.dao.*;
import net.learntechnology.examples.domain.Employee;
import net.learntechnology.examples.ext.stripes.GuiceInterceptor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

//~--- JDK imports ------------------------------------------------------------

import java.sql.SQLException;

import java.util.List;

/**
 *
 * @author nmaves
 */
@Singleton
public class EmployeeSqlMapDao implements EmployeeDao {
	private static final Log log = LogFactory.getLog(GuiceInterceptor.class);
	private SqlMapClient     sqlMapClient;

	@Inject
	public EmployeeSqlMapDao(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}

	public List<Employee> fetchAll() {
		try {
			return sqlMapClient.queryForList("Employee.getEmployee");
		} catch (SQLException e) {
			log.error("Could not query for employees", e);
			return null;
		}
	}

	public Employee fetch(final Integer id) {
		try {
			return (Employee) sqlMapClient.queryForObject("Employee.getEmployee", id);
		} catch (SQLException e) {
			log.error("Could not query for employee", e);
			return null;
		}
	}

	public Integer insert(Employee employee) {
		try {
			return (Integer)sqlMapClient.insert("Employee.insertEmployee", employee);
		} catch (SQLException e) {
			log.error("Could not save employee", e);
			return null;
		}
	}


	public void update(Employee employee) {
		try {
			sqlMapClient.update("Employee.updateEmployee", employee);
		} catch (SQLException e) {
			log.error("Could not save employee", e);
		}
	}

	public void delete(Integer id) {
		try {
			sqlMapClient.delete("Employee.deleteEmployee", id);
		} catch (SQLException e) {
			log.error("Could not delete employee", e);
		}
	}
}
